#include <string.h>
#include "ESP01s_Control.h"
volatile uint8_t ESP_index = 0;
volatile ESP_State_t ESP_state = ESP_state_START;

/*
//	AT回复的标准格式：
//	AT\r\n
//	\r\n
//	OK\r\n

//	USART_SendString(&USART2_Cfg, "ATE0\r\n");  			// 关闭回显

//  链接热点
//		失败代码：
//			中间响应（阶段失败）
//				1 链接超时
//				2 密码错误
//				3 找不到目标AP
//				4 其它错误
//			最终响应（完全失败）
//				FAIL
//		成功代码：
//			中间响应
//				WIFI CONNECTED
//				WIFI GOT IP
//				ok
//			最终响应
//				FAIL

*/

/*
// 阿里云：/root/3th/9080/stm32_-project/01_test/02_Server_ESP01_chat/Server
// <记得开热点>
*/
void ESP_StControl(void) {
	USART_SendBuffer(&USART1_Cfg, (uint8_t*)"[TEST]ESP_stindex\r\n", strlen("[TEST]ESP_stindex\r\n"));
	
//	USART_SendBuffer(&USART1_Cfg, (uint8_t*)"AT\r\n", 4);	// 可用，通过测试
//	USART_SendBuffer(&USART2_Cfg, (uint8_t*)"AT\r\n", 4);
	
	const char* start_cmd[] = {
		"AT\r\n",
		"AT+CWMODE_DEF=1\r\n",
		"AT+CWJAP_DEF=\"yang\",\"12345678\"\r\n",
		"AT+CIPSTART=\"TCP\",\"47.111.20.8\",9080\r\n",	
		"AT+CIPMODE=1\r\n",
		"AT+CIPSEND\r\n", 
		NULL,
	};
	
	if(start_cmd[ESP_index] != NULL) {
		USART_SendBuffer(&USART2_Cfg, (const uint8_t*)start_cmd[ESP_index], strlen(start_cmd[ESP_index])); 
		ESP_index++;
	}
	else{
		ESP_state = ESP_STATE_TRANSPARENT;
		USART_printf(&USART1_Cfg, "[TEST]Enter TRANSPARENT Mode\r\n");
		GPIO_WriteBit(LED_PORT, LED1_PIN, (BitAction)LED_ON);
	}
}

void ESP_EdControl(void){
	USART_SendBuffer(&USART1_Cfg, (uint8_t*)"[TEST]ESP_edindex, %d\r\n", strlen("[TEST]ESP_edindex, %d\r\n"));
	
	const char* end_cmd[] = {
		"+++",
		"AT+CIPMODE=0\r\n",
		"AT+CIPCLOSE\r\n",
		NULL,
	};
	
	if(end_cmd[ESP_index] != NULL) {
		USART_SendBuffer(&USART2_Cfg,  (const uint8_t*)end_cmd[ESP_index], strlen(end_cmd[ESP_index])); 
		ESP_index++;
	}
	else{
		USART_printf(&USART1_Cfg, "[TEST]Connect Broken\r\n");
		GPIO_WriteBit(LED_PORT, LED1_PIN, (BitAction)LED_OFF);
	}
}

/*
STATUS:2​​ : 已连接WiFi，未连接服务器
​​STATUS:3​​ : 已连接TCP服务器
​​STATUS:5​​ : 透传模式激活
*/
void ESP_Status(void){
	USART_SendBuffer(&USART2_Cfg, (uint8_t*)"AT+CIPSTATUS\r\n", strlen("AT+CIPSTATUS\r\n"));	// 查询链接状态
}

void ESP_SendPacket(void) {
    char payload[MAX_MSG_LEN];
	const char *device_state;
	
	// 1. 读取状态
	uint8_t pin_state = GPIO_ReadOutputDataBit(BUZZER_PORT, BUZZER_PIN);
	device_state = (pin_state == (BitAction)Buzzer_OFF) ? "Buzzer_OFF" : "Buzzer_ON";
	
	// 2. 构建完整HTTP请求
    int payload_len = snprintf(payload, sizeof(payload), "GET /0 HTTP/1.1\r\nuser-agent: STM32\r\n\r\n[Buzzer_state:%s]", device_state);
	
	// 3. 发送原始数据
	// Linux乱码（后面发现其实不是乱码，是vim显示原因）
	payload[payload_len] = '\0';
    USART_printf(&USART2_Cfg, payload);  	
	
	// 发不出去
//	for (int i = 0; i < payload_len; i++) {	
//		USART_SendByte(&USART2_Cfg, payload[i]);
//		while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
//	}

//	// 不行
//	payload[payload_len] = '\0';
//	for(int i = payload_len + 1; i < MAX_MSG_LEN; i++){
//		payload[i] = 0;
//	}
//	USART_printf(&USART2_Cfg, payload); 
	
//	// 还未测试
//	char payload_new[payload_len + 1];
//	strncpy(payload_new, payload, payload_len + 1);
//	payload_new[payload_len + 1] = '\0';
//	USART_printf(&USART2_Cfg, payload_new); 

	// 还未测试，需要修改函数
//	USART_SendData(&USART2_Cfg, (uint8_t*)payload, len); // 精确发送有效长度

	LED_shortBlink();
}

/*
// 不行
void ESP_SendPacket(void) {
    uint8_t pin_state = GPIO_ReadOutputDataBit(BUZZER_PORT, BUZZER_PIN);
    const char *state = (pin_state == Buzzer_ON) ? "ON" : "OFF";
    
    // 动态计算所需空间
    int need_len = snprintf(NULL, 0, "BZ:%s\r\n", state) + 1;
    char *payload = malloc(need_len);
    snprintf(payload, need_len, "BZ:%s\r\n", state);

    // 发送并释放
    for (char *p = payload; *p; p++) USART_SendByte(&USART2_Cfg, *p);
    free(payload);
}
*/
